package com.bibrApp.difu.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;


import com.bibrApp.difu.R;
import com.bibrApp.difu.actvity.tibi_mvp.AddrBean;
import com.bibrApp.difu.adapter.PopupAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 添加银行卡,添加地址弹窗
 *
 */
public class AddressPopup extends PopupWindow {
    private Context mContext;
    private PopupAdapter mAdapter;
    private OnPopupCallBack mCallback;

    private List<AddrBean> mDatas = new ArrayList<>();


    public AddressPopup(Context context, OnPopupCallBack callBack) {
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, callBack);
    }

    @SuppressLint("InflateParams")
    @SuppressWarnings("deprecation")
    private AddressPopup(Context context, int width, int height, OnPopupCallBack callBack) {
        this.mContext = context;
        this.mCallback = callBack;

        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setWidth(width);
        setHeight(height);
        setBackgroundDrawable(new BitmapDrawable());
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.popup_spinner, null));
        initUI();
    }

    private void initUI() {

        RecyclerView popup_recycler = getContentView().findViewById(R.id.popup_recycler);
        popup_recycler.setLayoutManager(new LinearLayoutManager(mContext));
        popup_recycler.setHasFixedSize(true);
        mAdapter = new PopupAdapter();

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mCallback != null) {
                mCallback.onItemClick((AddrBean) adapter.getItem(position));
                dismiss();
            }
        });
       /* mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (mCallback != null && view.getId() == R.id.spinner_close) {
                PopupBean item = (PopupBean) adapter.getItem(position);
                mDatas.remove(item);
                mCallback.onDeleteClick(item);
                mAdapter.setNewData(mDatas);
            }
        });*/

        popup_recycler.setAdapter(mAdapter);
    }

    /**
     * @param bottomView bottomView
     */
    public void showBottom(View bottomView) {
        int[] bottomLocation = new int[2];
        bottomView.getLocationOnScreen(bottomLocation);
        int popupGravity = Gravity.NO_GRAVITY;
        showAtLocation(bottomView, popupGravity, bottomLocation[0], bottomView.getHeight() + bottomLocation[1]);
    }

   /* public void setBankData(List<BankAddressResBody.Data> data) {
        mDatas.clear();
        mDatas.addAll(data);
        mAdapter.setNewData(mDatas);
    }*/

    public void setAddressData(List<AddrBean> data) {
        mDatas.clear();
        mDatas.addAll(data);
        mAdapter.setNewData(mDatas);
    }

    public interface OnPopupCallBack {
        void onItemClick(AddrBean data);

        void onDeleteClick(AddrBean data);

    }

}
